<?php
include_once 'Nusoap073/nusoap.php';

class AllegroExportController {
	private $country;
	private $loginname;
	private $password;
	private $apiKey;
	private $client;
	private $session;
	private $version;
	private $czy_jest_nastepny_poziom;
	
	public $zalogowany;
	
	function __construct(){		
		
		$this->czy_jest_nastepny_poziom = true;
		$this->zalogowany = false;
	}
	
	public function loginToAllegro(){
		global $objSession;
		try{
			$this->session = $this->client->doLogin($this->loginname, $this->password, $this->country, $this->apiKey, $this->version['ver-key']);
			$objSession->allegro_loginname = $this->loginname;
			$objSession->allegro_password = $this->password;
			$objSession->country = $this->country;
			return true;
		}catch(SoapFault $soapFault){
			echo "nie zalogowany - proszę spróbowac jeszcze raz.";
			return false;
		}
		
	}
	
	public function connectToAllegroApi(){
		global $config,$objSession;
//			$par = array(0=>array('country'=>228,'loginname'=>'21w_test','password'=>'test123'),
//					 1=>array('country'=>1,'loginname'=>'21w','password'=>'silky123'),
//					 2=>array('country'=>1,'loginname'=>'zdzisiek82','password'=>'135ziko987'));


		$this->apiKey = '8fee66bb82';
		
		
		if ($config->__isset("allegro_loginname") && $config->__isset("allegro_password") && $config->__isset("country")){
			$this->loginname = $config->allegro_loginname; 
			$this->password = $config->allegro_password;
			$this->country = $config->country;
		}else if ($objSession->allegro_loginname != '' && $objSession->allegro_password != '' && $objSession->country != ''){
			$this->country = $objSession->country;   // Poland: 1
			$this->loginname = $objSession->allegro_loginname;
			$this->password = $objSession->allegro_password;
		}else{
			return false;
		}
		
		try{
			$this->client = new SoapClient('http://webapi.allegro.pl/uploader.php?wsdl');
			// pobieranie wersji WebAPI
			$this->version = $this->client->doQuerySysStatus(1, $this->country, $this->apiKey);
			return ( array_key_exists("info",$this->version) && array_key_exists("ver-key",$this->version)) ? true : false;
		}catch(SoapFault $soapFault){
			//echo $soapFault;
			return false;
		}
	}
	
	private function getCategories(){

		// do testow
//		return array ( 'cats-list' => 
//			array ( 
//				0 => new Categ( array("cat-id"=>100 ,"cat-name"=>'Antiques & Collectables', "cat-parent"=>0, 'cat-position'=>0) ), 
//				1 => new Categ( array('cat-id'=>101 ,'cat-name'=>'Books','cat-parent'=> 0, 'cat-position'=>1 )), 
//				2 => new Categ( array('cat-id'=>102 ,'cat-name'=>'Computers','cat-parent'=> 0, 'cat-position'=>2 )),
//				3 => new Categ( array('cat-id'=>103 ,'cat-name'=>'Consumer Electronics','cat-parent'=> 0, 'cat-position'=>3 )),
//				4 => new Categ( array('cat-id'=>104 ,'cat-name'=>'Films','cat-parent'=> 0, 'cat-position'=>4 )),
//				5 => new Categ( array('cat-id'=>105 ,'cat-name'=>'Drama','cat-parent'=> 104, 'cat-position'=>0 )),
//				6 => new Categ( array('cat-id'=>106 ,'cat-name'=>'Comedy','cat-parent'=> 104, 'cat-position'=>1 )),
//				7 => new Categ( array('cat-id'=>107 ,'cat-name'=>"Drama' history",'cat-parent'=> 105, 'cat-position'=>0 )),
//				8 => new Categ( array('cat-id'=>108 ,'cat-name'=>'Drama horror','cat-parent'=> 105, 'cat-position'=>1 )),
//				9 => new Categ( array('cat-id'=>109 ,'cat-name'=>'Wars','cat-parent'=> 107, 'cat-position'=>0 )),
//			),
//			'version' => '1.0.0'
//		);
//		
		
		return $this->client->doGetCatsData($this->country,$this->version['ver-key'],$this->apiKey);
	}

	public function pokazFormAllegro($categID=0,$parentID=0,$oferta){
		global $dispatcher,$config,$objSession,$parameters;

		if(is_file($parameters['allegro']['kategorie'])){	
			$categ = $this->czytajKategorieZPliku();

			if ( !is_array($categ)){
				$categ = $this->getCategories();
				
				// jesli lista kategori jest mala to zapisujemy do zmiennej sessji
				//$objSession->kategorie = $categ;
				
				//jesli duza jak np. allegro to do pliku
				$this->zapiszKategorieDoPliku($categ);
		
			}			
		}
		


		$selectedCateg = explode(",", AllegroExportController::selectedCategroyByUsers($categID,$categ));
//		$arr = Array();
//		foreach ($selectedCateg as $item) {
//			$arr[$item] = $item;
//		}

		$this->czyJestNastepnyPoziomDlaWybranejKategorii($categID,$categ);
		
		if( !$this->czy_jest_nastepny_poziom ) tools::_assign("wybranaKategoria",$categID);		
		tools::_assign("polozenieAllegro", AllegroExportController::categroyPolozenie($categID,$categ,$oferta));		
	
		tools::_display("AllegroExport/form_allegro_head.tpl");
		AllegroExportController::categroyTree($parentID,$categID,$categ,0,$oferta,$categID,$selectedCateg);		
		tools::_display("AllegroExport/form_allegro.tpl");				
		//tools::_display("Allegro/jedna_oferta_export.tpl");	
	}
	
	private function czytajKategorieZPliku(){
		global $parameters;
		if(is_file($parameters['allegro']['kategorie'])){	
			$fh = fopen($parameters['allegro']['kategorie'], 'r+');
			return unserialize(fread($fh, filesize($parameters['allegro']['kategorie'])));
		}
		return false;
	}
	
	private function zapiszKategorieDoPliku($categ){
		global $dispatcher;
		$log = new Logs_Logs($parameters['allegro']['kategorie']);
		$log->add(serialize($categ));
	}
	
	public function categroyTree($id_kategoria_parent,$id_kategoria_back,$category,$poziom = 0,$oferta='',$curentID=0,$celectedIDs=null){
		global $config;
		if ($poziom == 0 ) echo "<a href=\"{$config->page},,exportuj_oferte,{$oferta},{$id_kategoria_parent}.html\"><< powrót</a>";
		$i = 0;
		$sub = 'open';
		$catname = 'cat-name';
		$catid = 'cat-id';
		$catparent = 'cat-parent';
		$czy_jest_nastepny_poziom = false;
		foreach ($category['cats-list'] as $key1 => $rows) {
			//if ( $rows->$catparent == $id_kategoria_parent &&  $poziom == 0 || $id_kategoria_parent == $curentID && $rows->$catparent == $curentID &&  $poziom == 1){
			if ( $rows->$catparent == 0 && $poziom == 0 && $id_kategoria_parent == 0 ||  $rows->$catid == $celectedIDs[1] && $poziom == 0 || $rows->$catparent == $id_kategoria_parent && in_array($rows->$catparent,$celectedIDs) && $poziom > 0 ){
				$czy_jest_nastepny_poziom = true;
				if ($sub == 'open'){ 
					echo "<ul id=\"cat{$rows->$catid}\" class=\"sidemenu\">";
					$sub = 'close';				
				}
				echo "<li> ";
				$export =  (!$this->czy_jest_nastepny_poziom && $curentID == $rows->$catid ) ? " doexportu" : "";
				$selected = (in_array($rows->$catid,$celectedIDs)) ? "class=\"selected {$export}\"" : "";

				echo "<a {$selected} href=\"{$config->page},,exportuj_oferte,{$oferta},{$rows->$catid},{$rows->$catparent},".tools::__link($rows->$catname).".html\">{$rows->$catname}</a>";
				$this->categroyTree($rows->$catid,$rows->$catparent,$category, $poziom+1,$oferta,$curentID,$celectedIDs);
				
				echo '</li>';
				$i++;
			}
		}			
		if ($sub == 'close') echo '</ul>';
	}
	
	
	
	public static function categroyPolozenie($id_kategoria,$category,$oferta=''){
		global $config;
		$catname = 'cat-name';
		$catid = 'cat-id';
		$catparent = 'cat-parent';
		$czy_jest_nastepny_poziom = false;
		foreach ($category['cats-list'] as $key1 => $rows) {
			if ( $rows->$catid == $id_kategoria){	
				$czy_jest_nastepny_poziom = true;
				$cat = AllegroExportController::categroyPolozenie($rows->$catparent,$category,$oferta);
				$cat .= " &raquo; ";
				$cat .= "<a href=\"{$config->page},,exportuj_oferte,{$oferta},{$rows->$catid},{$rows->$catparent}.html\">{$rows->$catname}</a>";
				return $cat;
			}
		}			
	}
	
	public static function selectedCategroyByUsers($id_kategoria,$category){
		global $config;
		$catname = 'cat-name';
		$catid = 'cat-id';
		$catparent = 'cat-parent';
		$czy_jest_nastepny_poziom = false;
		foreach ($category['cats-list'] as $key1 => $rows) {
			if ( $rows->$catid == $id_kategoria){	
				$czy_jest_nastepny_poziom = true;
				$cat = AllegroExportController::selectedCategroyByUsers($rows->$catparent,$category);
				$cat .= ",";
				$cat .= "{$rows->$catid}";
				return $cat;
			}
		}			
	}
	
	
	private function czyJestNastepnyPoziomDlaWybranejKategorii($id_kategoria,$category){
		global $config;
		$catname = 'cat-name';
		$catid = 'cat-id';
		$catparent = 'cat-parent';
		$this->czy_jest_nastepny_poziom = false;
		foreach ($category['cats-list'] as $key1 => $rows) {
			if ( $rows->$catparent == $id_kategoria){	
				$this->czy_jest_nastepny_poziom = true;
				break;
			}
		}
	}
	
	
	public function zapiszAukcjeAllegro(){
		
		$param=array();
		$param[0]=array('fid'=>1, 'fvalue-string'=>'Nowy przedmiot - rower', 'fvalue-int'=>'', 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[1]=array('fid'=>2, 'fvalue-string'=>'', 'fvalue-int'=>1865, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[2]=array('fid'=>3, 'fvalue-string'=>'', 'fvalue-int'=>'', 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>time(), 'fvalue-boolean'=>'');
		$param[3]=array('fid'=>4, 'fvalue-string'=>'', 'fvalue-int'=>2, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[4]=array('fid'=>5, 'fvalue-string'=>'', 'fvalue-int'=>1, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[5]=array('fid'=>6, 'fvalue-string'=>'', 'fvalue-int'=>'', 'fvalue-float'=>'1.00', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[6]=array('fid'=>7, 'fvalue-string'=>'', 'fvalue-int'=>'', 'fvalue-float'=>'0.00', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[7]=array('fid'=>8, 'fvalue-string'=>'', 'fvalue-int'=>'', 'fvalue-float'=>'0.00', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[8]=array('fid'=>9, 'fvalue-string'=>'', 'fvalue-int'=>228, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[9]=array('fid'=>10, 'fvalue-string'=>'', 'fvalue-int'=>216, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[10]=array('fid'=>11, 'fvalue-string'=>'Wroclaw', 'fvalue-int'=>'', 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[11]=array('fid'=>12, 'fvalue-string'=>'buyer', 'fvalue-int'=>0, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[12]=array('fid'=>13, 'fvalue-string'=>'', 'fvalue-int'=>2, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[13]=array('fid'=>14, 'fvalue-string'=>'', 'fvalue-int'=>1, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[14]=array('fid'=>15, 'fvalue-string'=>'', 'fvalue-int'=>0, 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[15]=array('fid'=>24, 'fvalue-string'=>'Opis :-)', 'fvalue-int'=>'', 'fvalue-float'=>'', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		$param[16]=array('fid'=>25, 'fvalue-string'=>'', 'fvalue-int'=>'', 'fvalue-float'=>'9.50', 'fvalue-image'=>'', 'fvalue-text'=>'', 'fvalue-datetime'=>'', 'fvalue-boolean'=>'');
		 
		//$parameters = array($this->session, $param, 0, 1);
		 
		//$soapclient = new soapclient('http://webapi.allegro.pl/uploader.php?wsdl','wsdl');
		echo $this->client->doNewAuctionExt($this->session['session-handle-part'],$param,0,1);
	}
}

class Categ{
	
	function __construct($arr){
		$this->__set('cat-id',$arr['cat-id']);
		$this->__set('cat-name',$arr['cat-name']);
		$this->__set('cat-parent',$arr['cat-parent']);
		$this->__set('cat-position',$arr['cat-position']);
	}
	function __set($var,$value){
		$this->$var = $value;
	}
	
}

/*
 * 

Array ( [cats-list] => 
	Array ( 
		[0] => stdClass Object ( 
			[cat-id] => 1800 
			[cat-name] => Antiques & Collectables 
			[cat-parent] => 0 
			[cat-position] => 0 ) 
		[1] => stdClass Object ( 
			[cat-id] => 1801 
			[cat-name] => Books 
			[cat-parent] => 0 
			[cat-position] => 1 ) 
		[2] => stdClass Object ( 
			[cat-id] => 1802 
			[cat-name] => Computers 
			[cat-parent] => 0 
			[cat-position] => 2 ) 
		[3] => stdClass Object ( 
			[cat-id] => 1804 
			[cat-name] => Consumer Electronics 
			[cat-parent] => 0 
			[cat-position] => 3 ) 
		[4] => ...
*/



